#include "../server.hpp"

class EchoServer
{
private:
    TcpServer _server;
private:
    void OnConnected(const PtrConnection& conn) 
    {
        DBG_LOG("NEW CONNECTION:%p", conn.get());
    }
    void OnClosed(const PtrConnection& conn)
    {
        DBG_LOG("CLOSE CONNECTION:%p", conn.get());
    }
    void OnMessage(const PtrConnection& conn, Buffer* buf)  //消息到来，连接读事件激活，该如何处理该连接
    {
        conn->Send(buf->ReadPosition(), buf->ReadAbleSize());
        buf->MoveReadOffset(buf->ReadAbleSize());
        // conn->Shutdown();
    }
public:
    EchoServer(int port):_server(port)
    {
        TcpServer server(8085);
        _server.SetThreadCount(2);
        _server.EnableInactiveRelease(10);
        _server.SetClosedCallback(std::bind(&EchoServer::OnClosed, this, std::placeholders::_1));
        _server.SetConnectedCallback(std::bind(&EchoServer::OnConnected, this, std::placeholders::_1));   //这就和std::bind(_1_2)一样，个数和类型并没有和定义的函数有冲突的地方，所以不需要bind预留的方式
        _server.SetMessageCallback(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
    }
    void Start()
    {
        _server.Start();
    }
};